<?php
/**
 * 角色管理
 */

class Setting_RoleManageController extends Cms_AdminController {
	
	/**
	 * 初始化
	 */
	public function init() {
		parent::init ();
	}
	
	public function indexAction() {
		$this->view->setScriptFile ( 'admin/admin_common.js' );
		
		$DB_Role = new DB_AdminRole ();
		$role_list = $DB_Role->loadList ( "*", "", "listorder DESC, roleid DESC" );
		
		$this->view->assign ( 'role_list', $role_list );
		
		$this->render ( 'Setting/adminsetting/rolemanage', null, true );
	}
	
	/**
	 * 添加角色
	 */
	public function addAction() {
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		
		if (isset ( $dosubmit )) {
			$info = $this->_request->getPost ( 'info' );
			
			$Model_AdminRole = new Model_AdminRole ();
			if ($Model_AdminRole->add_role ( $info )) {
				$this->view->perRedirect ( '数据添加成功！', '/setting/rolemanage/' );
			} else {
				$this->view->perRedirect ( '数据添加失败！', '/setting/rolemanage/add/' );
			}
		} else {
			$this->view->setScriptFile ( 'formvalidator.js' );
			$this->view->setScriptFile ( 'formvalidatorregex.js' );
			
			$this->render ( 'Setting/adminsetting/roleadd', null, true );
		}
	}
	
	/**
	 * 编辑角色
	 */
	public function editAction() {
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		
		if (isset ( $dosubmit )) {
			$info = $this->_request->getPost ( 'info' );
			$roleid = $this->_request->getPost ( 'roleid' );
			
			$Model_AdminRole = new Model_AdminRole ();
			if ($Model_AdminRole->edit_role ( $roleid, $info )) {
				$this->view->perRedirect ( '数据修改成功！', '/setting/rolemanage/' );
			} else {
				$this->view->perRedirect ( '数据修改失败！', '/setting/rolemanage/add/' );
			}
		} else {
			$roleid = $this->_request->get ( 'roleid' );
			
			$DB_Role = new DB_AdminRole ();
			$Role_info = $DB_Role->loadInfoByPrimary ( $roleid );
			
			$this->view->setScriptFile ( 'formvalidator.js' );
			$this->view->setScriptFile ( 'formvalidatorregex.js' );
			
			$this->view->assign ( 'Role_info', $Role_info );
			
			$this->render ( 'Setting/adminsetting/roleedit', null, true );
		}
	}
	
	/**
	 * 删除角色
	 */
	public function deleteAction() {
		$roleid = $this->_request->get ( 'roleid' );
		
		$Model_AdminRole = new Model_AdminRole ();
		if ($Model_AdminRole->delete_role ( $roleid )) {
			$this->view->perRedirect ( '删除成功！', '/setting/rolemanage/' );
		} else {
			$this->view->perRedirect ( '删除成功！', '/setting/rolemanage/' );
		}
	}
	
	/**
	 * 权限设置
	 */
	public function privsettingAction() {
		$roleid = $this->_request->get ( 'roleid' );
		
		$this->view->assign ( 'roleid', $roleid );
		
		$DB_Role = new DB_AdminRole ();
		$role_list = $DB_Role->loadList ( "*", "", "listorder DESC, roleid DESC" );
		
		$this->view->assign ( 'role_list', $role_list );
		
		$this->render ( 'Setting/adminsetting/privsetting', null, true );
	}
	
	public function roleprivAction() {
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		
		if (isset ( $dosubmit )) {
			$roleid = $this->_request->getPost ( 'roleid' );
			$menuid = $this->_request->getPost ( 'menuid' );
			
			$menu_array = serialize ( $menuid );
			
			$DB_AdminRolePriv = new DB_AdminRolePriv ();
			$Role_count = $DB_AdminRolePriv->loadCount ( '*', "roleid={$roleid}" );
			if ($Role_count > 0) {
				if ($DB_AdminRolePriv->updateByConditions ( array ('data' => $menu_array ), "roleid={$roleid}" )) {
					$this->view->perRedirect ( '操作成功!', "/setting/rolemanage/rolepriv/roleid/{$roleid}/" );
				} else {
					$this->view->perRedirect ( '操作失败!', "/setting/rolemanage/rolepriv/roleid/{$roleid}/" );
				}
			} else {
				if ($DB_AdminRolePriv->insert ( array ('roleid' => $roleid, 'data' => $menu_array ) )) {
					$this->view->perRedirect ( '操作成功!', "/setting/rolemanage/rolepriv/roleid/{$roleid}/" );
				} else {
					$this->view->perRedirect ( '操作失败!', "/setting/rolemanage/rolepriv/roleid/{$roleid}/" );
				}
			}
		} else {
			$roleid = $this->_request->get ( 'roleid' );
			
			$this->view->setScriptFile ( '/admin/jquery.treetable.js' );
			$this->view->setCssFile ( '/jquery.treeTable.css' );
			
			$tree = new Cms_Tree ();
			$tree->icon = array ('│ ', '├─ ', '└─ ' );
			$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
			
			$DB_Menu = new DB_Menu ();
			$result = $DB_Menu->loadList ( '*', "", "listorder ASC,id DESC" );
			
			$DB_AdminRolePriv = new DB_AdminRolePriv ();
			$AdminRolePriv = $DB_AdminRolePriv->loadInfo ( "data", "roleid={$roleid}" );
			if ($AdminRolePriv) {
				$data = unserialize ( $AdminRolePriv ['data'] );
			} else {
				$data = array ();
			}
			
			$Admin_Role = new Model_AdminRole ();
			
			foreach ( $result as $n => $t ) {
				$result [$n] ['checked'] = (in_array ( $t ['id'], $data )) ? ' checked' : '';
				$result [$n] ['level'] = $Admin_Role->get_level ( $t ['id'], $result );
				$result [$n] ['parentid_node'] = ($t ['parentid']) ? ' class="child-of-node-' . $t ['parentid'] . '"' : '';
			}
			$str = "<tr id='node-\$id' \$parentid_node>
		<td style='padding-left:30px;'>\$spacer<input type='checkbox' name='menuid[]' value='\$id' level='\$level' \$checked onclick='javascript:checknode(this);'> \$name</td>
		</tr>";
			
			$tree->init ( $result );
			$categorys = $tree->get_tree ( 0, $str );
			
			$this->view->assign ( 'categorys', $categorys );
			
			$this->view->assign ( 'roleid', $roleid );
			$this->render ( 'Setting/adminsetting/rolepriv', null, true );
		}
	}
	
	/**
	 * 栏目权限
	 */
	public function settingcatprivAction() {
		$roleid = $this->_request->get ( 'roleid' );
		
		$this->view->assign ( 'roleid', $roleid );
		
		$DB_Role = new DB_AdminRole ();
		$role_list = $DB_Role->loadList ( "*", "", "listorder DESC, roleid DESC" );
		
		$this->view->assign ( 'role_list', $role_list );
		$this->render ( 'Setting/adminsetting/settingcatpriv', null, true );
	}
	
	/**
	 * 栏目权限
	 */
	public function columnprivAction() {
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		
		if (isset ( $dosubmit )) {
			$roleid = $this->_request->getPost ( 'roleid' );
		
		} else {
			$roleid = $this->_request->get ( 'roleid' );
			
			$this->view->setScriptFile ( '/admin/jquery.treetable.js' );
			$this->view->setCssFile ( '/jquery.treeTable.css' );
			
			$this->view->assign ( 'roleid', $roleid );
			$this->render ( 'Setting/adminsetting/columnpriv', null, true );
		}
	}
	
	/**
	 * 修改状态
	 */
	public function changestatusAction() {
		$roleid = $this->_request->get ( 'roleid' );
		$disabled = $this->_request->get ( 'disabled' );
		
		$DB_Role = new DB_AdminRole ();
		$return = $DB_Role->updateByConditions ( array ('disabled' => $disabled ), "roleid={$roleid}" );
		
		if ($return) {
			$this->view->perRedirect ( '操作成功！', '/setting/rolemanage/' );
		} else {
			$this->view->perRedirect ( '操作成功！', '/setting/rolemanage/' );
		}
	}
	
	/**
	 * 排序
	 */
	public function listorderAction() {
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		
		if (isset ( $dosubmit )) {
			$listorders = $this->_request->getPost ( 'listorders' );
			if (is_array ( $listorders )) {
				$DB_Role = new DB_AdminRole ();
				foreach ( $listorders as $roleid => $listorder ) {
					$DB_Role->updateByPrimary ( array ('listorder' => $listorder ), $roleid );
				}
				$this->view->perRedirect ( '操作成功！', '/setting/rolemanage/' );
			}
			$this->view->perRedirect ( '操作失败！', '/setting/rolemanage/' );
		}
		
		$this->view->perRedirect ( '操作失败！', '/setting/rolemanage/' );
	}
}
?>